using UnityEngine;

public class PlaneMoveController : MonoBehaviour
{
	public static PlaneMoveController instance;

	public Transform m_transform;

	public bool accflagstate;

	public bool decflagstate;

	private Vector3 body_startpos;

	public Transform bodypoint;

	public bool island;

	public bool isdied;

	private float rotationx;

	private float rotationz;

	public float speed;

	public float uplift;

	public float rightleftsoft;

	private float rightleftsoftabs;

	private float bodyZ_soft;

	private float divesalto;

	private float diveblocker;

	public AnimationCurve rotateCurve;

	public float Maxspeed;

	public float TakeoffSpeed;

	public float Maxspeedlimit;

	public float accSpeed;

	public float rotation_base_x = 100f;

	public float rotation_base_y_di = 40f;

	public float rotation_base_y = 150f;

	public float body_rotation_z = 100f;

	private PosRot startpos = new PosRot();

	private bool isoutrange;

	private Vector3 rangevec = default(Vector3);

	private float backtime;

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
		m_transform = base.transform;
		groundtrigger.triggered = false;
		island = true;
	}

	public void initinfo(ctl_att curctl_att)
	{
		if (GlobalInf.CurrentQuality < Quality.Low)
		{
			TakeoffSpeed = 120f;
			Maxspeedlimit = 160f;
			Maxspeed = 180f;
			accSpeed = 25f;
			rotation_base_x = 30f;
			rotation_base_y = 30f;
			body_rotation_z = 70f;
		}
		else
		{
			TakeoffSpeed = curctl_att.takeoffspeed;
			Maxspeedlimit = curctl_att.maxspeedlimit;
			Maxspeed = curctl_att.maxspeed;
			accSpeed = curctl_att.accspeed;
			rotation_base_x = curctl_att.rotatex;
			rotation_base_y = curctl_att.rotatey;
			body_rotation_z = curctl_att.bodyz;
		}
	}

	private void OnDestroy()
	{
		if (instance != null)
		{
			instance = null;
		}
	}

	private void Start()
	{
		body_startpos = bodypoint.localPosition;
		startpos.pos = m_transform.position;
		startpos.rot = m_transform.eulerAngles;
	}

	public void init_posrot(PosRot curpr)
	{
		if (curpr.pos.y < 50f)
		{
			speed = 0f;
		}
		else
		{
			speed = TakeoffSpeed;
		}
		m_transform.position = curpr.pos;
		m_transform.rotation = Quaternion.Euler(curpr.rot);
		isdied = false;
		startpos = curpr;
		plane_smothfollow.instance.m_Transform.position = curpr.pos;
		plane_smothfollow.instance.m_Transform.rotation = Quaternion.Euler(curpr.rot);
	}

	public void reset_pos()
	{
		if (startpos.pos.y < 100f)
		{
			m_transform.position = startpos.pos + Vector3.up * 300f;
		}
		else
		{
			m_transform.position = startpos.pos;
		}
		m_transform.rotation = Quaternion.Euler(startpos.rot);
		isdied = false;
		plane_smothfollow.instance.m_Transform.position = startpos.pos;
		plane_smothfollow.instance.m_Transform.rotation = Quaternion.Euler(startpos.rot);
		groundtrigger.triggered = false;
	}

	private void LateUpdate()
	{
		if (isdied || !GlobalInf.loading_over)
		{
			return;
		}
		if (!isoutrange)
		{
			rangevec = m_transform.position - GlobalInf.centerpoint;
			rangevec.y = 0f;
			if (!(rangevec.magnitude > (float)GlobalInf.flyrange))
			{
				Vector3 position = m_transform.position;
				if (!(position.y > 4000f))
				{
					goto IL_0263;
				}
			}
			isoutrange = true;
			backtime = 0f;
			MonoInstance<AudioController>.instance.play(AudioType.warning, 0f);
			UIController.instance.show_hitinfo("你飞出了近战范围，请返回！");
			UIController.instance.hitinfo_appear();
		}
		else
		{
			backtime += Time.deltaTime;
			if (backtime > 10f)
			{
				if (startpos.pos.y > 100f)
				{
					m_transform.position = startpos.pos;
					m_transform.rotation = Quaternion.Euler(startpos.rot);
					plane_smothfollow.instance.m_Transform.position = startpos.pos;
					plane_smothfollow.instance.m_Transform.rotation = Quaternion.Euler(startpos.rot);
				}
				else
				{
					m_transform.position = startpos.pos + Vector3.up * 600f;
					m_transform.rotation = Quaternion.Euler(startpos.rot);
					plane_smothfollow.instance.m_Transform.position = startpos.pos;
					plane_smothfollow.instance.m_Transform.rotation = Quaternion.Euler(startpos.rot);
				}
			}
			rangevec = m_transform.position - GlobalInf.centerpoint;
			rangevec.y = 0f;
			if (rangevec.magnitude < (float)GlobalInf.flyrange)
			{
				Vector3 position2 = m_transform.position;
				if (position2.y < 4000f)
				{
					isoutrange = false;
					MonoInstance<AudioController>.instance.stop(AudioType.warning);
					UIController.instance.hitinfo_disappear();
				}
			}
		}
		goto IL_0263;
	IL_0263:
		android_controller();
		if (groundtrigger.triggered)
		{
			if (speed < 1f && !island)
			{
				Vector3 position3 = m_transform.position;
				if (position3.y < 100f)
				{
					island = true;
					GamePlayerprefs.instance.saveLandachi();
				}
			}
		}
		else
		{
			Vector3 position4 = m_transform.position;
			if (position4.y > 100f && island)
			{
				island = false;
			}
		}
		if (speed <= TakeoffSpeed)
		{
			MonoInstance<AudioController>.instance.setpitch(speed / TakeoffSpeed);
		}
	}

	public void unity_controller()
	{
		if (speed > 12f)
		{
			UIController uIController = UIController.instance;
			int num = (int)(speed * 8f - Mathf.Abs(UnityEngine.Input.GetAxis("Horizontal")) * 20f);
			Vector3 position = m_transform.position;
			uIController.showspeedandheight(num, (int)position.y);
		}
		else
		{
			UIController uIController2 = UIController.instance;
			int num2 = (int)(speed * 8f);
			Vector3 position2 = m_transform.position;
			uIController2.showspeedandheight(num2, (int)position2.y);
		}
		Vector3 eulerAngles = m_transform.eulerAngles;
		rotationx = eulerAngles.x;
		Vector3 localEulerAngles = bodypoint.localEulerAngles;
		rotationz = localEulerAngles.z;
		if (groundtrigger.triggered)
		{
			if (speed > 1f)
			{
				m_transform.Rotate(0f, rotateCurve.Evaluate(UnityEngine.Input.GetAxis("Horizontal")) * Time.deltaTime * rotation_base_y_di, 0f, Space.World);
			}
		}
		else
		{
			if (speed > TakeoffSpeed * 0.75f)
			{
				if (UnityEngine.Input.GetAxis("Vertical") <= 0f)
				{
					m_transform.Rotate(rotateCurve.Evaluate(UnityEngine.Input.GetAxis("Vertical")) * Time.deltaTime * rotation_base_x, 0f, 0f);
				}
				if (UnityEngine.Input.GetAxis("Vertical") > 0f)
				{
					m_transform.Rotate((0.8f - divesalto) * rotateCurve.Evaluate(UnityEngine.Input.GetAxis("Vertical")) * Time.deltaTime * rotation_base_x, 0f, 0f);
				}
			}
			m_transform.Rotate(0f, Time.deltaTime * rotation_base_y * rotateCurve.Evaluate(UnityEngine.Input.GetAxis("Horizontal")), 0f, Space.World);
			bodypoint.transform.Rotate(0f, 0f, Time.deltaTime * body_rotation_z * (1f - rightleftsoftabs - diveblocker) * rotateCurve.Evaluate(UnityEngine.Input.GetAxis("Horizontal")) * -1f, Space.Self);
		}
		if (UnityEngine.Input.GetAxis("Horizontal") <= 0f && rotationz > 0f && rotationz < 90f)
		{
			rightleftsoft = rotationz * 2.2f / 100f * -1f;
		}
		if (UnityEngine.Input.GetAxis("Horizontal") >= 0f && rotationz > 270f)
		{
			rightleftsoft = 7.92f - rotationz * 2.2f / 100f;
		}
		if (rightleftsoft > 1f)
		{
			rightleftsoft = 1f;
		}
		if (rightleftsoft < -1f)
		{
			rightleftsoft = -1f;
		}
		if ((double)rightleftsoft > -0.01 && (double)rightleftsoft < 0.01)
		{
			rightleftsoft = 0f;
		}
		rightleftsoftabs = Mathf.Abs(rightleftsoft);
		if (rotationx < 90f)
		{
			divesalto = rotationx / 100f;
		}
		if (rotationx > 90f)
		{
			divesalto = -0.2f;
		}
		if (rotationx < 90f)
		{
			diveblocker = rotationx / 200f;
		}
		else
		{
			diveblocker = 0f;
		}
		if (rotationz < 180f && UnityEngine.Input.GetAxis("Horizontal") > 0f)
		{
			bodypoint.transform.Rotate(0f, 0f, rightleftsoft * Time.deltaTime * body_rotation_z, Space.Self);
		}
		if (rotationz > 180f && UnityEngine.Input.GetAxis("Horizontal") < 0f)
		{
			bodypoint.transform.Rotate(0f, 0f, rightleftsoft * Time.deltaTime * body_rotation_z, Space.Self);
		}
		if (!Input.GetButton("Horizontal"))
		{
			if (rotationz < 135f)
			{
				bodypoint.transform.Rotate(0f, 0f, rightleftsoftabs * Time.deltaTime * (0f - body_rotation_z), Space.Self);
			}
			if (rotationz > 225f)
			{
				bodypoint.transform.Rotate(0f, 0f, rightleftsoftabs * Time.deltaTime * body_rotation_z, Space.Self);
			}
		}
		if (groundtrigger.triggered)
		{
			Transform transform = m_transform;
			Vector3 eulerAngles2 = m_transform.eulerAngles;
			transform.rotation = Quaternion.Euler(0f, eulerAngles2.y, 0f);
		}
		m_transform.Translate(0f, 0f, speed * Time.deltaTime);
		if (groundtrigger.triggered)
		{
			if (UnityEngine.Input.GetKey(KeyCode.Z) && speed < Maxspeed)
			{
				speed += Time.deltaTime * accSpeed;
			}
			if (!Input.GetKey(KeyCode.Z) && speed > 0f)
			{
				speed -= Time.deltaTime * accSpeed;
			}
		}
		else
		{
			if (UnityEngine.Input.GetKey(KeyCode.Z) && speed < Maxspeed)
			{
				speed += Time.deltaTime * accSpeed;
			}
			if (UnityEngine.Input.GetKey(KeyCode.X) && speed > TakeoffSpeed * 0.8f)
			{
				speed -= Time.deltaTime * accSpeed;
			}
			if (!Input.GetKey(KeyCode.X) && !Input.GetKey(KeyCode.Z))
			{
				if (speed < TakeoffSpeed)
				{
					speed += Time.deltaTime * accSpeed;
				}
				else if (speed > Maxspeedlimit)
				{
					speed -= Time.deltaTime * accSpeed;
				}
			}
		}
		if (speed < 0f)
		{
			speed = 0f;
		}
		m_transform.Translate(0f, uplift * Time.deltaTime / 5f, 0f);
		if (groundtrigger.triggered)
		{
			if (UnityEngine.Input.GetKey(KeyCode.Z))
			{
				if (TakeoffSpeed < speed)
				{
					uplift = speed - TakeoffSpeed;
				}
			}
			else
			{
				uplift = 0f;
			}
		}
		else
		{
			Vector3 position3 = m_transform.position;
			if (position3.y < 200f)
			{
				uplift = 0f - TakeoffSpeed + speed;
			}
			else if (speed < TakeoffSpeed)
			{
				uplift = 0f - TakeoffSpeed + speed;
			}
			else
			{
				uplift = 0f;
			}
		}
		if (UnityEngine.Input.GetKey(KeyCode.Z))
		{
			Transform transform2 = bodypoint;
			float x = body_startpos.x;
			float y = body_startpos.y;
			Vector3 localPosition = bodypoint.localPosition;
			transform2.localPosition = new Vector3(x, y, Mathf.Lerp(localPosition.z, body_startpos.z + 3f, Time.deltaTime * 1f));
		}
		if (UnityEngine.Input.GetKey(KeyCode.X) || !Input.GetKey(KeyCode.Z))
		{
			Transform transform3 = bodypoint;
			float x2 = body_startpos.x;
			float y2 = body_startpos.y;
			Vector3 localPosition2 = bodypoint.localPosition;
			transform3.localPosition = new Vector3(x2, y2, Mathf.Lerp(localPosition2.z, body_startpos.z, Time.deltaTime * 1f));
		}
	}

	public void android_controller()
	{
		Vector3 eulerAngles = m_transform.eulerAngles;
		rotationx = eulerAngles.x;
		Vector3 localEulerAngles = bodypoint.localEulerAngles;
		rotationz = localEulerAngles.z;
		if (GlobalInf.PlaneCtrlType == PLANECTRLTYPE.BUTTON)
		{
			if (speed > 12f)
			{
				UIController uIController = UIController.instance;
				int num = (int)(speed * 8f - Mathf.Abs(Joystick.joystickAxis.x) * 20f);
				Vector3 position = m_transform.position;
				uIController.showspeedandheight(num, (int)position.y);
			}
			else
			{
				UIController uIController2 = UIController.instance;
				int num2 = (int)(speed * 8f);
				Vector3 position2 = m_transform.position;
				uIController2.showspeedandheight(num2, (int)position2.y);
			}
			if (groundtrigger.triggered)
			{
				if (speed > 1f)
				{
					m_transform.Rotate(0f, rotateCurve.Evaluate(Joystick.joystickAxis.x) * Time.deltaTime * rotation_base_y_di, 0f, Space.World);
				}
			}
			else
			{
				if (speed > TakeoffSpeed * 0.75f)
				{
					if (Joystick.joystickAxis.y <= 0f)
					{
						m_transform.Rotate(Joystick.joystickAxis.y * Time.deltaTime * rotation_base_x, 0f, 0f);
					}
					if (Joystick.joystickAxis.y > 0f)
					{
						m_transform.Rotate((0.8f - divesalto) * Joystick.joystickAxis.y * Time.deltaTime * rotation_base_x, 0f, 0f);
					}
				}
				m_transform.Rotate(0f, Time.deltaTime * rotation_base_y * rotateCurve.Evaluate(Joystick.joystickAxis.x), 0f, Space.World);
				bodypoint.transform.Rotate(0f, 0f, Time.deltaTime * body_rotation_z * (1f - rightleftsoftabs - diveblocker) * Joystick.joystickAxis.x * -1f, Space.Self);
			}
			if (Joystick.joystickAxis.x <= 0f && rotationz > 0f && rotationz < 90f)
			{
				rightleftsoft = rotationz * 2.2f / 100f * -1f;
			}
			if (Joystick.joystickAxis.x >= 0f && rotationz > 270f)
			{
				rightleftsoft = 7.92f - rotationz * 2.2f / 100f;
			}
			if (rightleftsoft > 1f)
			{
				rightleftsoft = 1f;
			}
			if (rightleftsoft < -1f)
			{
				rightleftsoft = -1f;
			}
			if ((double)rightleftsoft > -0.01 && (double)rightleftsoft < 0.01)
			{
				rightleftsoft = 0f;
			}
			rightleftsoftabs = Mathf.Abs(rightleftsoft);
			if (rotationx < 90f)
			{
				divesalto = rotationx / 100f;
			}
			if (rotationx > 90f)
			{
				divesalto = -0.2f;
			}
			if (rotationx < 90f)
			{
				diveblocker = rotationx / 200f;
			}
			else
			{
				diveblocker = 0f;
			}
			if (rotationz < 180f && Joystick.joystickAxis.x > 0f)
			{
				bodypoint.transform.Rotate(0f, 0f, rightleftsoft * Time.deltaTime * body_rotation_z, Space.Self);
			}
			if (rotationz > 180f && Joystick.joystickAxis.x < 0f)
			{
				bodypoint.transform.Rotate(0f, 0f, rightleftsoft * Time.deltaTime * body_rotation_z, Space.Self);
			}
			if (Joystick.joystickAxis.x == 0f)
			{
				if (rotationz < 135f)
				{
					bodypoint.transform.Rotate(0f, 0f, rightleftsoftabs * Time.deltaTime * (0f - body_rotation_z), Space.Self);
				}
				if (rotationz > 225f)
				{
					bodypoint.transform.Rotate(0f, 0f, rightleftsoftabs * Time.deltaTime * body_rotation_z, Space.Self);
				}
			}
		}
		else
		{
			if (speed > 12f)
			{
				UIController uIController3 = UIController.instance;
				int num3 = (int)(speed * 8f - Mathf.Abs(Joystick.gravityAxis.y) * 20f);
				Vector3 position3 = m_transform.position;
				uIController3.showspeedandheight(num3, (int)position3.y);
			}
			else
			{
				UIController uIController4 = UIController.instance;
				int num4 = (int)(speed * 8f);
				Vector3 position4 = m_transform.position;
				uIController4.showspeedandheight(num4, (int)position4.y);
			}
			if (groundtrigger.triggered)
			{
				if (speed > 1f)
				{
					m_transform.Rotate(0f, Joystick.gravityAxis.y * Time.deltaTime * rotation_base_y_di, 0f, Space.World);
				}
			}
			else
			{
				if (speed > TakeoffSpeed * 0.75f)
				{
					if (Joystick.gravityAxis.x <= 0f)
					{
						m_transform.Rotate(Joystick.gravityAxis.x * Time.deltaTime * rotation_base_x, 0f, 0f);
					}
					if (Joystick.gravityAxis.x > 0f)
					{
						m_transform.Rotate((0.8f - divesalto) * Joystick.gravityAxis.x * Time.deltaTime * rotation_base_x, 0f, 0f);
					}
				}
				m_transform.Rotate(0f, Time.deltaTime * rotation_base_y * Joystick.gravityAxis.y, 0f, Space.World);
				bodypoint.transform.Rotate(0f, 0f, Time.deltaTime * body_rotation_z * (1f - rightleftsoftabs - diveblocker) * Joystick.gravityAxis.y * -1f, Space.Self);
			}
			if (Joystick.gravityAxis.y <= 0f && rotationz > 0f && rotationz < 90f)
			{
				rightleftsoft = rotationz * 2.2f / 100f * -1f;
			}
			if (Joystick.gravityAxis.y >= 0f && rotationz > 270f)
			{
				rightleftsoft = 7.92f - rotationz * 2.2f / 100f;
			}
			if (rightleftsoft > 1f)
			{
				rightleftsoft = 1f;
			}
			if (rightleftsoft < -1f)
			{
				rightleftsoft = -1f;
			}
			if ((double)rightleftsoft > -0.01 && (double)rightleftsoft < 0.01)
			{
				rightleftsoft = 0f;
			}
			rightleftsoftabs = Mathf.Abs(rightleftsoft);
			if (rotationx < 90f)
			{
				divesalto = rotationx / 100f;
			}
			if (rotationx > 90f)
			{
				divesalto = -0.2f;
			}
			if (rotationx < 90f)
			{
				diveblocker = rotationx / 200f;
			}
			else
			{
				diveblocker = 0f;
			}
			if (rotationz < 180f && Joystick.gravityAxis.y > 0f)
			{
				bodypoint.transform.Rotate(0f, 0f, rightleftsoft * Time.deltaTime * body_rotation_z, Space.Self);
			}
			if (rotationz > 180f && Joystick.gravityAxis.y < 0f)
			{
				bodypoint.transform.Rotate(0f, 0f, rightleftsoft * Time.deltaTime * body_rotation_z, Space.Self);
			}
			if (Joystick.gravityAxis.y == 0f)
			{
				if (rotationz < 135f)
				{
					bodypoint.transform.Rotate(0f, 0f, rightleftsoftabs * Time.deltaTime * (0f - body_rotation_z), Space.Self);
				}
				if (rotationz > 225f)
				{
					bodypoint.transform.Rotate(0f, 0f, rightleftsoftabs * Time.deltaTime * body_rotation_z, Space.Self);
				}
			}
		}
		if (groundtrigger.triggered)
		{
			Transform transform = m_transform;
			Vector3 eulerAngles2 = m_transform.eulerAngles;
			transform.rotation = Quaternion.Euler(0f, eulerAngles2.y, 0f);
		}
		m_transform.Translate(0f, 0f, speed * Time.deltaTime);
		if (groundtrigger.triggered)
		{
			if (accflagstate && speed < Maxspeed)
			{
				speed += Time.deltaTime * accSpeed;
			}
			if (!accflagstate && speed > 0f)
			{
				speed -= Time.deltaTime * accSpeed;
			}
		}
		else
		{
			if (accflagstate && speed < Maxspeed)
			{
				speed += Time.deltaTime * accSpeed;
			}
			if (decflagstate && speed > TakeoffSpeed * 0.8f)
			{
				speed -= Time.deltaTime * accSpeed;
			}
			if (!decflagstate && !accflagstate)
			{
				if (speed < TakeoffSpeed)
				{
					speed += Time.deltaTime * accSpeed;
				}
				else if (speed > Maxspeedlimit)
				{
					speed -= Time.deltaTime * accSpeed;
				}
			}
		}
		if (speed < 0f)
		{
			speed = 0f;
		}
		m_transform.Translate(0f, uplift * Time.deltaTime / 5f, 0f);
		if (groundtrigger.triggered)
		{
			if (accflagstate)
			{
				if (TakeoffSpeed < speed)
				{
					uplift = speed - TakeoffSpeed;
				}
			}
			else
			{
				uplift = 0f;
			}
		}
		else
		{
			Vector3 position5 = m_transform.position;
			if (position5.y < 200f)
			{
				uplift = 0f - TakeoffSpeed + speed;
			}
			else if (speed < TakeoffSpeed)
			{
				uplift = 0f - TakeoffSpeed + speed;
			}
			else
			{
				uplift = 0f;
			}
		}
		if (accflagstate)
		{
			Transform transform2 = bodypoint;
			float x = body_startpos.x;
			float y = body_startpos.y;
			Vector3 localPosition = bodypoint.localPosition;
			transform2.localPosition = new Vector3(x, y, Mathf.Lerp(localPosition.z, body_startpos.z + 3f, Time.deltaTime * 1f));
		}
		if (decflagstate || !accflagstate)
		{
			Transform transform3 = bodypoint;
			float x2 = body_startpos.x;
			float y2 = body_startpos.y;
			Vector3 localPosition2 = bodypoint.localPosition;
			transform3.localPosition = new Vector3(x2, y2, Mathf.Lerp(localPosition2.z, body_startpos.z, Time.deltaTime * 1f));
		}
	}
}
